package org.richin.office.util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

import com.torchbox.f5.spider.ConvertedDocument;
import com.torchbox.f5.spider.converters.DocumentConverter;

import net.matuschek.http.HttpDoc;

import junit.framework.TestCase;

/**
 * Tests for DocumentConverter
 */
public class TestDocumentConverter extends TestCase {
	private String sampleFilePath;

	protected void setUp() throws Exception {
		super.setUp();
		
		sampleFilePath = System.getProperty("samples.path");
	}
	
	/**
	 * Builds an HttpDoc from the supplied file
	 */
	public static HttpDoc buildDoc(File f) throws Exception {
		HttpDoc doc = new HttpDoc();
		doc.setURL(f.toURL());
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		FileInputStream fin = new FileInputStream(f);
		
		byte[] b = new byte[8192];
		int read = 0;
		while( (read = fin.read(b)) > -1 ) {
			if(read > 0) {
				baos.write(b, 0, read);
			}
		}
		
		doc.setContent(baos.toByteArray());
		
		return doc;
	}

	public void dtestStatic() {
		// Test the static stuff
		DocumentConverter.class.getName();
	}
	
	public void testGetAllKnownConverters() {
		// Test that we have them, and they're not null
		DocumentConverter[] convs = DocumentConverter.getAllKnownConverters();
		assertTrue(convs.length > 0);
		
		for(int i=0; i<convs.length; i++) {
			assertNotNull(convs[i]);
		}
	}
	
	public void dtestGetByContentType() {
		// Existing ones
		assertEquals(
				DocumentConverter.getConverterForContentType("text/html").getClass().getName(),
				"com.torchbox.f5.spider.converters.HTMLConverter"
		);
		assertEquals(
				DocumentConverter.getConverterForContentType("application/msword").getClass().getName(),
				"com.torchbox.f5.spider.converters.WordConverter"
		);
		
		// Non existant one
		assertNull(DocumentConverter.getConverterForContentType("foo/bar"));
	}
	
	public void dtestGetByFileExtension() {
		// Existing ones
		assertEquals(
				DocumentConverter.getConverterForFileExtension("HTML").getClass().getName(),
				"com.torchbox.f5.spider.converters.HTMLConverter"
		);
		assertEquals(
				DocumentConverter.getConverterForFileExtension("pdf").getClass().getName(),
				"com.torchbox.f5.spider.converters.PDFConverter"
		);
		
		// Non existant one
		assertNull(DocumentConverter.getConverterForFileExtension("foobar"));
	}
	
	/**
	 * Tests the word extractor
	 */
	public void testWord() throws Exception {
		ConvertedDocument cd;
		DocumentConverter conv =
			DocumentConverter.getConverterForFileExtension("doc");
		
		//File doc = new File(sampleFilePath + File.separator + "SampleDoc.doc");
		File docx = new File("E:/Funambol/tools/tomcat/temp/笑话011656842218170407438742902.docx");
		File[] files = new File[] { docx }; 

		for (int i = 0; i < files.length; i++) {
			cd = new ConvertedDocument();
			conv.convertDocument(
					buildDoc(files[i]), cd
			);
			
			System.out.println(cd.getContent());
			
		}
	}
	
	public void dtestExcel() throws Exception {
		ConvertedDocument cd;
		DocumentConverter conv =
			DocumentConverter.getConverterForFileExtension("xls");
		
		File xls = new File(sampleFilePath + File.separator + "SampleSS.xls");
		File xlsx = new File(sampleFilePath + File.separator + "SampleSS.xlsx");
		
		// TODO - fix the xlsx extractor to work!
		File[] files = new File[] { xls, xlsx }; 

		for (int i = 0; i < files.length; i++) {
			cd = new ConvertedDocument();
			conv.convertDocument(
					buildDoc(files[i]), cd
			);
			assertEquals("Sample Spreadsheet", cd.getTitle());
			assertEquals("Spreadsheet for testing", cd.getSummary());
			assertEquals("Nick Burch", cd.getAuthor());
			
			String text = cd.getContent().replace("\r", "").replace("\t\n", "\n");
			System.out.println(text.length());
			System.out.println(text);
			assertTrue(text.startsWith("First Sheet\nTest spreadsheet\n2nd row\t2nd row 2nd column\n"));
			
			// Formatting fun...
			if(text.indexOf("13.0") == -1) {
				assertTrue(text.endsWith("13\nSheet3\n"));
			} else {
				assertTrue(text.endsWith("13.0\nSheet3\n"));
			}
			
			assertTrue(text.length() >= 200);
			assertTrue(text.length() <= 210);
		}
	}
	
	public void dtestPowerPoint() throws Exception {
		ConvertedDocument cd;
		DocumentConverter conv =
			DocumentConverter.getConverterForFileExtension("ppt");
		
		File xls = new File(sampleFilePath + File.separator + "SampleShow.ppt");
		File xlsx = new File(sampleFilePath + File.separator + "SampleShow.pptx");
		File[] files = new File[] { xls, xlsx };
		
		String[] starts = new String[] {
				"Title of the first slide\nSubtitle of the first slide\n",
				"Title of the first slide\nSubtitle of the first slide\n"
		};
		String[] ends = new String[] {
				"THIS LINE IS BOLD\n*\n",
				"THIS LINE IS BOLD\n\n"
		};

		for (int i = 0; i < files.length; i++) {
			cd = new ConvertedDocument();
			conv.convertDocument(
					buildDoc(files[i]), cd
			);
			assertEquals("SlideShow Sample", cd.getTitle());
			assertEquals("A sample slideshow", cd.getSummary());
			assertEquals("Nick Burch", cd.getAuthor());
			
			String text = cd.getContent().replace("\r", "");
			System.out.println(text.length());
			System.out.println(text);
			assertTrue(text.startsWith(starts[i]));
			assertTrue(text.endsWith(ends[i]));
			
			assertTrue(text.length() >= 312);
			assertTrue(text.length() <= 315);
		}
	}
}

